package StudyCode.summary.myfile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.Set;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ManagingAttributesAndPermissions {
	public static void main(String[] args) {
		Path path = Paths.get("E:\\迅雷下载\\test1.txt");
		try {
			PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

			PosixFileAttributes attributes = view.readAttributes();
			Set<PosixFilePermission> permissions = attributes.permissions();

			for (PosixFilePermission permission : permissions) {
				System.out.print(permission.toString() + ' ');
			}
			System.out.println();

			FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions
					.asFileAttribute(permissions);
			Set<PosixFilePermission> fileAttributeSet = fileAttributes.value();
			for (PosixFilePermission posixFilePermission : fileAttributeSet) {
				System.out.print(posixFilePermission.toString() + ' ');
			}

			System.out.println();
			System.out.println(PosixFilePermissions.toString(permissions));
			permissions = PosixFilePermissions.fromString("rw-rw-r--");
			for (PosixFilePermission permission : permissions) {
				System.out.print(permission.toString() + ' ');
			}
			System.out.println();

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
